<?php

/**
 * @Author:      zwh
 * @DateTime:    2016-12-7 10:24:07
 * @Description: 班组管理
 */
class ScsTeamController extends BaseController
{
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-12 11:34:18
     * @Description: 创建业务提成项目
     */
    public function create(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'name' => 'max:20|required',
                //'note' => 'max:40|required',
                'status'=> 'required',
                'type_id'=>'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ts=new Teams;
            $ts->name=Input::get('name');
            $ts->note=Input::get('note');
            $ts->status=Input::get('status');
            $ts->shop_id=$sc_user->shop_id;
            $ts->type_id=Input::get('type_id');
            $ts->save();
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "create", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-12-9 12:01:08
     * @Description: 修改班组信息
     */
    public function updateTeam(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',
                'name' => 'max:20|required',
                //'note' => 'max:40|required',
                'status'=> 'required',
                'type_id'=>'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $id=Input::get('id');
            $me=MaintenanceEngineers::where('type','shop')->where('type_id',$sc_user->shop_id)->where('status',1)->select('team')->get()->toArray();
            $sc=SCUser::where('shop_id',$sc_user->shop_id)->where('status',1)->select('team')->get()->toArray();
            $me=array_column($me,'team');
            $sc=array_column($sc,'team');
            if(Input::get('status')==0){
                if(in_array($id,$me) || in_array($id,$sc)){
                    $response = Lang::get("scs_error.team_in_used_error_90014");
                    break;
                }
            }
            $ts=Teams::find(Input::get('id'));
            $ts->name=Input::get('name');
            $ts->note=Input::get('note');
            $ts->status=Input::get('status');
            $ts->type_id=Input::get('type_id');
            $ts->save();
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "updateTeam", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-12-7 10:24:39
     * @Description: 员工列表
     */
    public function employeeList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            
            $me=MaintenanceEngineers::where('type','shop')->where('type_id',$sc_user->shop_id)->where('status',1)->select('id','name','phone','team','account');//->get();
            $sc=SCUser::where('shop_id',$sc_user->shop_id)->where('status',1)->select('id','real_name','phone','team','user_name');//->get();
            if(Input::get("name")!=""){
                $me=$me->where("name",'like','%'.Input::get('name').'%');
                $sc=$sc->where("real_name",'like','%'.Input::get('name').'%');
            }
            if(Input::get("team")!=""){
                $me=$me->where("team",Input::get('team'));
                $sc=$sc->where("team",Input::get('team'));
            }
            $me=$me->get();
            $sc=$sc->get();
            $team=array();
            if(Input::get("type")==1){
                foreach($me as $_me){
                    $team[]=array('id'=>$_me->id,'name'=>$_me->name,'phone'=>$_me->phone,'team'=>$_me->team,'type'=>1,'user_name'=>$_me->account);
                }
            }else if(Input::get("type")==2){
                foreach($sc as $_sc){
                    $team[]=array('id'=>$_sc->id,'name'=>$_sc->real_name,'phone'=>$_sc->phone,'team'=>$_sc->team,'type'=>2,'user_name'=>$_sc->user_name);
                }
            }else{
                foreach($me as $_me){
                    $team[]=array('id'=>$_me->id,'name'=>$_me->name,'phone'=>$_me->phone,'team'=>$_me->team,'type'=>1,'user_name'=>$_me->account);
                }
                foreach($sc as $_sc){
                    $team[]=array('id'=>$_sc->id,'name'=>$_sc->real_name,'phone'=>$_sc->phone,'team'=>$_sc->team,'type'=>2,'user_name'=>$_sc->user_name);
                }
            }
            $count=count($team);
            $team=array_slice($team, ($page-1)*$page_size, $page_size);
            $ts=Teams::where('shop_id',$sc_user->shop_id)->where('status',1)->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"employee"=>$team,"team"=>$ts));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "employeeList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-12-7 14:12:03
     * @Description: 保存用户班组信息
     */
    public function update(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',
                'type' => 'required',
                'team' => 'required'
            )
        );
        $id=Input::get('id');
        $type=Input::get('type');
        $team=Input::get('team');
        $response = array('code' => 0, 'msg' => 'ok','result'=>array());
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            if($type==1){
                $me=MaintenanceEngineers::find($id);
                if(is_null($me)){
                    $response = Lang::get("scs_error.engineer_error_22006");
                    break;
                }
                $me->team=$team;
                $me->save();
            }else{
                $sc=SCUser::find($id);
                if(is_null($sc)){
                    $response = Lang::get("scs_error.scs_user_not_exiet21009");
                    break;
                }
                $sc->team=$team;
                $sc->save();
            }
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "update", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
        /**
     * @Author:      zwh
     * @DateTime:    2016-12-7 10:24:39
     * @Description: 班组列表
     */
    public function teamList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            
            $ts=Teams::where('shop_id',$sc_user->shop_id);
            if(Input::get("name")!=""){
                $ts=$ts->where("name",'like','%'.Input::get('name').'%');
            }
            $count=$ts->count();
            $ts=$ts->skip($page_size*($page-1))->take($page_size)->get();
            
            $tt=TeamType::all();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"team"=>$ts,'team_type'=>$tt));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "teamList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}